package com.jy.api.controller.SalesStoreController;

import com.jy.api.dao.StoreDao;
import com.jy.api.dao.StoreProfileDao;
import com.jy.api.domain.*;
import com.jy.api.service.ProductService;
import com.jy.api.service.StoreService;
import com.jy.api.service.TableSalesCodeService;
import com.jy.api.signUpReq.SalesCode.UpdateSalesCodeReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiaohua on 2019/4/4.
 */
@Slf4j
@RestController
@RequestMapping(value = "v1/sales/code")
public class SalesStoreController {


    @Autowired
    private TableSalesCodeService tableSalesCodeService;
    @Autowired
    private StoreService storeService;
    @Autowired
    private StoreDao storeDao;
    @Autowired
    private StoreProfileDao profileDao;

    @GetMapping("find-all")
    public ResponseEntity findAllCode(@RequestParam("mobile") String mobile, Pageable pageable) {
        return ResponseEntity.ok(tableSalesCodeService.findAllCode(mobile, pageable));
    }

    @PostMapping("update-code")
    public ResponseEntity updateCode(@RequestBody UpdateSalesCodeReq req) {
        return ResponseEntity.ok(tableSalesCodeService.updateCode(req));
    }

    @PostMapping("add-code")
    public ResponseEntity addCode(@RequestBody UpdateSalesCodeReq req) {
        return ResponseEntity.ok(tableSalesCodeService.addCode(req));
    }

    @GetMapping("my-store")
    public ResponseEntity getMyStore(@RequestParam("userName") String userName) {
        return ResponseEntity.ok(storeService.getMyStore(userName));
    }

    @GetMapping("all-store")
    public ResponseEntity getAllStore(@RequestParam("userName") String userName, Pageable pageable) {
        return ResponseEntity.ok(storeService.getAllStore(userName, pageable));
    }

    @PostMapping("update-store")
    public ResponseEntity updateStore(@RequestBody UpdateSalesCodeReq req) {
        return ResponseEntity.ok(storeService.updateStoreExp(req));
    }

    //回去所有店铺
    @GetMapping("list")
    public ResponseEntity getAllList(@RequestParam("userName") String userName, Pageable pageable) {
        return ResponseEntity.ok(storeService.getALLProfile(userName, pageable));
    }

    //商户秘钥
    @GetMapping("store-search")
    public ResponseEntity storeSearch(@RequestParam("name") String name) {
        return ResponseEntity.ok(storeService.findByStoreNameLike(name));
    }

    @GetMapping("one")
    public ResponseEntity storeOne(@RequestParam("storeId") String storeId) {
        return ResponseEntity.ok(storeDao.findOne(storeId));
    }

    @GetMapping("update-status")
    public ResponseEntity updateStatus(@RequestParam("id") String id, @RequestParam("enabled") boolean enabled) {
        StoreProfile storeProfile = profileDao.findOne(id);
        if (enabled) {
            storeProfile.setStatus(StoreProfileStatus.APPROVED);
        } else {
            storeProfile.setStatus(StoreProfileStatus.SUBMITTED);
        }
        profileDao.save(storeProfile);
        return ResponseEntity.ok().build();
    }


    @PostMapping("mch-key")
    public ResponseEntity updateMchKey(@RequestBody StoreMchAndKeyReq req) {
        Store store = storeDao.findOne(req.getId());
        store.setMchId(req.getMchId());
        store.setKey(req.getKey());
        return ResponseEntity.ok(storeDao.save(store));
    }


    @GetMapping("find-like")
    public ResponseEntity findLike(@RequestParam(value = "mobile", required = false) String mobile, @RequestParam(value = "name", required = false) String name) {
        List<String> strings = new ArrayList<>();
        List<Store> storeList;
        if (null == name || "".equals(name)) {
            storeList = storeDao.findByUsernameLike(mobile);
        } else {
            storeList = storeDao.findByStoreNameLike(name);
        }
        for (Store store : storeList) {
            strings.add(store.getId());
        }
        List<StoreProfile> profiles = profileDao.findByStoreIdIn(strings);
        return ResponseEntity.ok(profiles);
    }








}
